   //
   //  VisitorView.swift
   //  01-界面搭建
   //
   //  Created by Apple on 15/11/3.
   //  Copyright © 2015年 Apple. All rights reserved.
   //
   
   import UIKit
   import SnapKit
   //   //MARK: - 定义协议
   //   protocol VisitorDelegate:NSObjectProtocol {
   //   //注册
   //   func visitorViewDidRegister()
   //   //登录
   //   func visitorViewDidLogin()
   //   }
   class VisitorView: UIView {
    //    ///代理
    //    weak var delegate:VisitorDelegate?
    //    //MARK: - 监听方法
    //    @objc private func clickLogin() {
    //
    //    delegate?.visitorViewDidLogin()
    //
    //    }
    //    @objc private func clickRegister() {
    //
    //        delegate?.visitorViewDidRegister()
    //
    //    }
    
    // MARK: - 设置视图信息
    /// 设置视图信息
    ///
    /// - parameter imageName: 图片名称，首页设置为 nil
    /// - parameter title:     消息文字
    func setupInfo(imageName:String?,title:String) {
        
        messageLabel.text = title
        // 如果图片名称为 nil，说明是首页，直接返回
        guard let imgName = imageName else {
            startAnim()
            return
        }
        iconView.image = UIImage(named: imgName)
        //隐藏小房子
        homeIconView.hidden = true
        // 将遮罩图像移动到底层
        sendSubviewToBack(maskIconView)
    }
    /// 开启首页转轮动画
    private func startAnim() {
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 40
        // 用在不断重复的动画上，当动画绑定的图层对应的视图被销毁，动画会自动被销毁
        anim.removedOnCompletion = false
        // 添加到图层
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    //代码创建视图
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    //sb或xib创建视图
    required init?(coder aDecoder: NSCoder) {
        //当使用sb是程序崩溃
        // fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        setupUI()
    }
    //MARK: - 懒加载控件
    /// 图标，使用 image: 构造函数创建的 imageView 默认就是 image 的大小
    private lazy var iconView:UIImageView = UIImageView(ImageName: "visitordiscover_feed_image_smallicon")
    ///遮罩图像
    private lazy var maskIconView:UIImageView = UIImageView(ImageName: "visitordiscover_feed_mask_smallicon")
    ///小房子
    private lazy var homeIconView:UIImageView = UIImageView(ImageName: "visitordiscover_feed_image_house")
    ///文字
    private lazy var messageLabel:UILabel = UILabel(Labeltext: "关注一些人，回这里看看有什么惊喜")
    ///注册按钮
    
    lazy var registerButton:UIButton = UIButton(title: "注册", color: UIColor.orangeColor(), imageName: "common_button_white_disable")
    ///登陆按钮
    lazy var LoginButton:UIButton = UIButton(title: "登录", color: UIColor.darkGrayColor(), imageName: "common_button_white_disable")
    
    
   }
   //MARK: - 自动布局子控件
   extension VisitorView {
    
    private func setupUI() {
        //1.添加子控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(LoginButton)
        for v in subviews {
        v.translatesAutoresizingMaskIntoConstraints = false
        }
        //2.布局
        //1.图标
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-60)
        }
        //2.小房子
        homeIconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.centerY.equalTo(iconView.snp_centerY)
        }
        
        //3.文字
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
            make.width.equalTo(224)
            make.height.equalTo(36)
        }
        //4.注册
        registerButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(messageLabel.snp_left)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(36)
        }
        //5.登陆
       
        LoginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLabel.snp_right)
            make.top.equalTo(registerButton.snp_top)
            make.width.equalTo(registerButton.snp_width)
            make.height.equalTo(registerButton.snp_height)
        }

        // 6. 遮罩图像
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.bottom.equalTo(registerButton.snp_top)
        }
       // 设置背景颜色 - 灰度图 R = G = B，在 UI 元素中，大多数都使用灰度图，或者纯色图(安全色)
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        //        // 3. 添加监听方法
        //        registerButton.addTarget(self, action: "clickRegister", forControlEvents: .TouchUpInside)
        //        LoginButton.addTarget(self, action: "clickLogin", forControlEvents: .TouchUpInside)
        
    }
   }